package Analysis;

import java.util.Random;

public class StatisticalCorrelationClass extends CorrelationAbstractClass{

	/*
	 * get Matrix x and y 
	 * */
	
	public static double[] GetCorrelation (double xMatrix[][], double yMatrix[][]) 
	{
		int classSize = xMatrix[0].length;
		int numberOfSample = xMatrix.length;
		double correlation[] = new double[classSize];
		double[] x = new double[numberOfSample];
		double[] y = new double[numberOfSample];
		for(int i=0 ; i<classSize; i++)
		{
			for(int j=0; j<numberOfSample ;j++)
			{
				x[j]= xMatrix[j][i];
				y[j]= yMatrix[j][i];
				correlation[i] = GetCorrelation(x, y);
			}
		}
		return null;
	}
	
	/**
	 * return correlation between x,y 
	 **/
	 public static double GetCorrelation (double[] x, double[] y) 
	 {
		 
		 int N= x.length;
		 double sumX = 0, sumY = 0, sumOfMultiplication=0;
		 
		 for(int i= 0 ; i<N ; i++)	
		 {
			 sumOfMultiplication += x[i] * y[i];
			 sumX += x[i]; 
			 sumY += y[i];
		 }
		 double average = (double)1.0 /(double) N;
		   
		 return (average*average * sumOfMultiplication - (average * sumX) * (average * sumY));
	 }
	 
	 
	 public void Test()
	 {
		 double x[]= new double [5]; 
		 double y[]= new double [5];
		 
		 Random random = new Random ();
		 random.setSeed(0);
		 System.out.println("x:  ");
		 
		 for(int i= 0 ;  i<5 ; i++)
		 {
			x[i]= random.nextDouble();
			System.out.print(x[i]+"  ");
		 }
		 

		 System.out.println();
		 System.out.println("y:  ");
		 
		 for(int i= 0 ;  i<5 ; i++)
		 {
			 y[i]= random.nextDouble();
			 System.out.print(y[i]+"  ");
		 }
		
		 System.out.println(); 
		 System.out.println("Correlation:  " +GetCorrelation(x, y));
	 }
}
